const { Generator } = require("../../models");
const { Op } = require("sequelize");
const { NotFoundError } = require("../../utils/errors");
const { paging } = require("../../utils/utils");

/**
 * 获取分页列表
 * @param req
 * @returns {Promise<{rows: Model[], count: number}>}
 */
exports.list = async (req) => {
  try {
    // 获取分页参数
    const { query, currentPage, pageSize, offset } = paging(req);
    const condition = {
      order: [["id", "ASC"]],
      limit: pageSize,
      offset: offset,
    };
    //查询条件
    getCondition(condition, query);
    // 分页查询
    const { rows, count } = await Generator.findAndCountAll(condition);
    return { rows, count, currentPage, pageSize };
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 获取所有代码生成
 * @param req
 * @returns {Promise< Model[] >}
 */
exports.all = async (req) => {
  try {
    const condition = {
      order: [["id", "ASC"]],
    };
    //查询条件
    getCondition(condition, req.query);
    return await Generator.findAll(condition);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 获取代码生成详情
 * @param id
 * @returns {Promise< Model<*, TModelAttributes>>}
 */
exports.details = async (id) => {
  try {
    return await this.getById(id);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 创建代码生成
 * @param body
 * @returns {Promise< Model< any, TModelAttributes>>}
 */
exports.create = async (body) => {
  try {
    return await Generator.create(body);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 删除代码生成
 * @param id
 * @returns {Promise< void>}
 */
exports.del = async (id) => {
  try {
    const generator = await this.getById(id);
    await generator.destroy();
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 更新代码生成
 * @param id
 * @param body
 * @returns {Promise< Model<*, TModelAttributes>>}
 */
exports.update = async (id, body) => {
  try {
    const generator = await this.getById(id);
    await generator.update(body);
    return generator;
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 根据id获取代码生成
 * @param id
 * @returns {Promise< Model< any, TModelAttributes>>}
 */
exports.getById = async (id) => {
  // 获取当前代码生成
  const generator = await Generator.findByPk(id);
  if (!generator) {
    throw new NotFoundError(`ID: ${id} not found`);
  }
  return generator;
};

/**
 * 获取条件
 * @param condition
 * @param query
 */
function getCondition(condition, query) {
  // 数据库表名
  if (query.tableName) {
    condition.where = {
      tableName: {
        [Op.like]: `%${query.tableName}%`,
      },
    };
  }

  // 模型名
  if (query.modelName) {
    condition.where = {
      modelName: {
        [Op.like]: `%${query.modelName}%`,
      },
    };
  }

  // 字段名
  if (query.fields) {
    condition.where = {
      fields: {
        [Op.like]: `%${query.fields}%`,
      },
    };
  }

  // 业务名
  if (query.title) {
    condition.where = {
      title: {
        [Op.like]: `%${query.title}%`,
      },
    };
  }

  // 模块名
  if (query.routerPath) {
    condition.where = {
      routerPath: {
        [Op.like]: `%${query.routerPath}%`,
      },
    };
  }
}
